package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjAccount;
import com.zb.entity.ZjSystemLoginLog;
import com.zb.mapper.ZjSystemLoginLogMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjAccountService;
import com.zb.service.ZjSystemLoginLogService;
import com.zb.vo.SystemLoginLogListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统登录日志表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-21
 */
@Service
public class ZjSystemLoginLogServiceImpl extends ServiceImpl<ZjSystemLoginLogMapper, ZjSystemLoginLog> implements ZjSystemLoginLogService {
    @Resource
    private ZjSystemLoginLogMapper systemLoginLogMapper;
    @Resource
    ZjAccountService accountService;
    // 登录成功
    @Override
    public void loginSuccess(String account, Integer type, String browserDevice, String mode, String ip) {
        ZjAccount phoneOrAccount = accountService.getByPhoneOrAccount(account);
        ZjSystemLoginLog log  = new ZjSystemLoginLog();
        log.setId(IdWorker.getId());
        log.setAccountId(phoneOrAccount.getId());
        log.setAccount(account);
        log.setName(phoneOrAccount.getName());
        log.setDevice(type);
        log.setBrowserDevice(browserDevice);
        log.setMode(mode);
        log.setIp(ip);
        log.setSuccess(true);
        log.setMessage("登录成功");
        this.save(log);
    }

    // 登录失败
    @Override
    public void loginError(String account, Integer type, String browserDevice, String mode, String ip, String message) {
        ZjAccount phoneOrAccount = accountService.getByPhoneOrAccount(account);
        ZjSystemLoginLog log  = new ZjSystemLoginLog();
        log.setId(IdWorker.getId());
        log.setAccountId(null == phoneOrAccount? 0L : phoneOrAccount.getId());
        log.setAccount(account);
        log.setName("");
        log.setDevice(type);
        log.setBrowserDevice(browserDevice);
        log.setMode(mode);
        log.setIp(ip);
        log.setSuccess(false);
        log.setMessage("登录失败提示："+ message);
        this.save(log);
    }

    // 获取登录日志列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo search) {
        PageUtil.setPageLimit(search);
        List<ZjSystemLoginLog> loginLogs = systemLoginLogMapper.getList(custAccId, search);
        return PageUtil.pageList(search.getPage(), search.getLimit(), systemLoginLogMapper.getListCount(custAccId, search), loginLogs.stream().map(log -> {
            SystemLoginLogListVo vo = new SystemLoginLogListVo();
            BeanUtil.copyProperties(log, vo);
            vo.setCreateTime(log.getGmtCreate());
            return vo;
        }).toList());
    }

    // 根据账号ID获取登录日志
    @Override
    public Map<String, Object> getLoginLog(Long custAccId, Long accountId, PageUtil pageUtil) {
        PageUtil.setPageLimit(pageUtil);
        List<ZjSystemLoginLog> loginLogs = systemLoginLogMapper.getLoginLog(custAccId, accountId, pageUtil);
        return PageUtil.pageList(pageUtil.getPage(), pageUtil.getLimit(), systemLoginLogMapper.getLoginLogCount(custAccId, accountId), loginLogs.stream().map(log -> {
            SystemLoginLogListVo vo = new SystemLoginLogListVo();
            BeanUtil.copyProperties(log, vo);
            vo.setCreateTime(log.getGmtCreate());
            return vo;
        }).toList());
    }
}
